package com.zbbt.sportslottery.ui.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gc.materialdesign.views.ButtonRectangle;
import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengUtil;

/**
 * 
 * @ClassName: MactchDataFragment
 * @Description: 赛事活动
 * @author：WWH
 * @date：2014-12-19 上午9:44
 * @version V1.0
 */
public class MatchDataFragment extends BaseMenuFragment {
	private WebView webView;
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private String url = "http://182.92.184.102:9090/portal/sporttery/sportteryAction!getSportteryList.action";
	private String titleText = "赛事数据";
	private String htmlTitle;
	private GestureDetector mGestureDetector;
	private boolean isFirst = true;
	private ProgressBarCircularIndetermininate mProgressBar;
	private TextView actionshare;
	private LinearLayout error_layout;// 无网络时 显示的界面
	private boolean isError = false;

	public MatchDataFragment() {}

	public static MatchDataFragment getInstance() {
		return new MatchDataFragment();

	}

	@SuppressLint("NewApi")
	@Override
	protected void initView(LayoutInflater inflater) {
		mView = inflater.inflate(R.layout.activity_gamedata, null);
		error_layout = (LinearLayout) findViewById(R.id.error_layout);
		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.mProgressBar);
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		actionshare = (TextView) findViewById(R.id.action_share);
		actionshare.setVisibility(View.GONE);
		webView = (WebView) findViewById(R.id.game_data_webview);
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		findViewById(R.id.toolbar).setVisibility(View.GONE);
		// 加载需要显示的网页
		webView.loadUrl(url);
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				if (isFirst) {
					mProgressBar.setVisibility(View.VISIBLE);
				} else {
					mSwipeRefreshLayout.setRefreshing(true);
				}
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				// TODO Auto-generated method stub
				view.loadUrl("javascript:document.body.innerHTML=\"" + "" + "\"");
				ToastUtil.showShort("网络连接失败,请稍后再试！");
				isError = true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				if (isError) {
					error_layout.setVisibility(View.VISIBLE);
				}else{
					error_layout.setVisibility(View.GONE);
				}
				mSwipeRefreshLayout.setRefreshing(false);
				mProgressBar.setVisibility(View.GONE);
				isFirst = false;
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url); // 在当前的webview中跳转到新的url
				return true;
			}

		});
		/**
		 * 获取标题
		 */
		webView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onReceivedTitle(WebView view, String title) {
				htmlTitle = title;
				super.onReceivedTitle(view, title);
			}
		});

		mGestureDetector = new GestureDetector(this.getActivity(), mOnGestureListener);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				webView.reload();

			}
		});

	}

	private OnGestureListener mOnGestureListener = new OnGestureListener() {

		@Override
		public boolean onDown(MotionEvent e) {
			return false;
		}

		@SuppressLint("NewApi")
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			boolean isXWell = Math.abs(e2.getX() - e1.getX()) < Constant.FLING_MIN_DISTANCE_X ? true : false;

			if (isXWell && e1.getY() - e2.getY() > Constant.FLING_MIN_DISTANCE
					&& Math.abs(velocityY) > Constant.FLING_MIN_VELOCITY) {
				MatchDataFragment.this.getActivity().getActionBar().hide();
			} else if (isXWell && e2.getY() - e1.getY() > Constant.FLING_MIN_DISTANCE
					&& Math.abs(velocityY) > Constant.FLING_MIN_VELOCITY) {
				MatchDataFragment.this.getActivity().getActionBar().show();
			}

			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {

		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}
	};

	@Override
	protected void setListener() {
		error_layout.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.error_layout:
			webView.reload();
			isError=false;
			break;

		default:
			break;
		}

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

}
